Skip to content

zlib,url: avoid flattening strings in value-returning fast API calls - #67536

Open
dasunNimantha wants to merge 3 commits into
nodejs:mainfrom
dasunNimantha:fast-api-no-gc
Open

dasunNimantha wants to merge 3 commits into
nodejs:mainfrom
dasunNimantha:fast-api-no-gc

Conversation

@dasunNimantha

Copy link
Copy Markdown

zlib.crc32(), zlib.crc32c() (string input) and URL.canParse() can abort the process with:

abort: Lazy deopt after a fast API call with return value is unsupported

This is the same crash as #66870, which #67136 fixed for Buffer.byteLength(). The fast callbacks of these APIs flatten or convert their string argument (Utf8Value / String::ValueView, and ToString() in canParse). Flattening a large non-flat string allocates; if that allocation triggers a full GC that invalidates the optimized caller (for example by clearing a weakly embedded map), V8 has to lazily deoptimize the caller once the fast call returns. V8 does not support that for fast calls that return a value and runs Builtin::kAbort instead (crbug.com/418936518, see FastApiCallReducerAssembler::ReduceFastApiCall in deps/v8/src/compiler/js-call-reducer.cc).

Changes:

  • zlib: string input to crc32()/crc32c() now goes through a regular binding. The fast bindings only take ArrayBufferViews, which are read without allocating.
  • url: the canParse() fast path now takes a sequential one-byte string (FastOneByteString), which V8 passes without allocating, and returns -1 for anything else so JavaScript calls the regular binding, the same approach as buffer: limit UTF-8 fast calls to safe one-byte strings #67136. ToString() is no longer called inside a fast callback.
  • test: test/parallel/test-fast-api-calls-gc-lazy-deopt.js optimizes a caller of each API, drops the only object with a weakly embedded map, and calls it with non-flat old-space strings so that a full GC runs while the string is read. It aborts on v27.0.0-alpha.0.1.0 and passes with this change.

I reproduced the abort on 25.0.0 to 26.13.1 and on v27.0.0-alpha.0.1.0 (crc32 and canParse; byteLength is already fixed there by #67136).

One note on URL.canParse(): the interpreter only ever sees the slow callback's -1, so the first optimized call at a call site deopts once (insufficient feedback for the result === 1 compare) and is then re-optimized.

canParse() gets faster for ASCII input, since the fast path no longer converts or copies the string. crc32()/crc32c() are unchanged for buffers and large strings; very short string inputs may be somewhat slower because strings no longer take the fast call. These numbers come from benchmark/compare.js with 8 runs on a laptop, so small differences are noise; a benchmark CI run would give firmer numbers.

image

permission.has() uses the same pattern (Utf8Value in a value-returning fast callback), but I have not reproduced the crash there, so it is not part of this PR.

AI disclosure: I used a coding agent to help with the root-cause analysis and the initial patch. I reviewed the change, reproduced the crash on the versions above, checked that the new test fails without the fix, and ran make lint, make format-cpp, and the zlib, url and Buffer.byteLength tests (parallel/test-zlib*, parallel/test-whatwg-url*, parallel/test-url*, parallel/test-buffer-bytelength*). I have not run the full make -j4 test.

Refs: #66870
Refs: #67136

@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

Review requested:

  • @nodejs/url

@nodejs-github-bot nodejs-github-bot added c++ Issues and PRs that require attention from people who are familiar with C++. needs-ci PRs that need a full CI run. typings Issues and PRs related to internal TypeScript declarations. whatwg-url Issues and PRs related to the WHATWG URL implementation. zlib Issues and PRs related to the zlib module and its compression dependencies. labels Nov 17, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Welcome to Node.js, and thank you for your first contribution!

Before review, please take a moment to read:

Please make sure every commit is signed off. For a first pull request, GitHub Actions require collaborator approval and Jenkins CI must be started by a collaborator or triager, so an initial wait is normal.

zlib.crc32() and zlib.crc32c() read string input through a V8 fast API
callback that returns a value. Reading a non-flat (cons) string flattens
it, which allocates on the JS heap. If that allocation triggers a full
GC that invalidates the optimized caller, V8 has to lazily deoptimize it
after the fast call returns, which it does not support for fast calls
with a return value: the process aborts with "Lazy deopt after a fast
API call with return value is unsupported".

Strings now go through a regular binding. The fast bindings are limited
to ArrayBufferViews, which are read without allocating.

Refs: nodejs#66870
Refs: nodejs#67136
Signed-off-by: dasunNimantha <dasun4@pm.me>
URL.canParse() converted and flattened its arguments inside a V8 fast
API callback that returns a value. Like zlib.crc32(), that can allocate
and trigger a GC that invalidates the optimized caller, which V8 then
cannot lazily deoptimize and aborts instead.

The fast path now takes a sequential one-byte string, which V8 passes
without allocating, and returns -1 for anything it cannot handle that
way so that JavaScript falls back to the regular binding.

Refs: nodejs#66870
Refs: nodejs#67136
Signed-off-by: dasunNimantha <dasun4@pm.me>
Optimize a caller of Buffer.byteLength(), zlib.crc32(), zlib.crc32c()
and URL.canParse() that embeds a map weakly, drop the only object with
that map, and call it with non-flat old-space strings so that a full GC
runs while the string is read. Without the fixes the process aborts
with "Lazy deopt after a fast API call with return value is
unsupported".

Refs: nodejs#66870
Signed-off-by: dasunNimantha <dasun4@pm.me>
@codecov

codecov Bot commented Nov 17, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 85.00000% with 9 lines in your changes missing coverage. Please review.
✅ Project coverage is 90.68%. Comparing base (bc6dd40) to head (e2157b9).
⚠️ Report is 30 commits behind head on main.

Files with missing lines Patch % Lines
src/node_zlib.cc 72.72% 2 Missing and 4 partials ⚠️
src/node_url.cc 83.33% 1 Missing and 2 partials ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##             main   #67536   +/-   ##
=======================================
  Coverage   90.67%   90.68%           
=======================================
  Files         800      800           
  Lines      280489   280516   +27     
  Branches    54166    54171    +5     
=======================================
+ Hits       254336   254384   +48     
+ Misses      16277    16253   -24     
- Partials     9876     9879    +3     
Files with missing lines Coverage Δ
lib/internal/url.js 93.76% <100.00%> (-0.15%) ⬇️
lib/zlib.js 98.42% <100.00%> (+0.01%) ⬆️
src/node_url.h 100.00% <ø> (ø)
src/node_url.cc 79.38% <83.33%> (+0.44%) ⬆️
src/node_zlib.cc 79.79% <72.72%> (-0.63%) ⬇️

... and 37 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

c++ Issues and PRs that require attention from people who are familiar with C++. needs-ci PRs that need a full CI run. typings Issues and PRs related to internal TypeScript declarations. whatwg-url Issues and PRs related to the WHATWG URL implementation. zlib Issues and PRs related to the zlib module and its compression dependencies.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants